package com.jiawa.train.member.service;

import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.CommonResp;
import com.jiawa.train.common.util.SnowUtil;
import com.jiawa.train.member.MemberRegisterReq;
import com.jiawa.train.member.domain.Member;
import com.jiawa.train.member.domain.MemberExample;

import com.jiawa.train.member.mapper.MemberMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author xxx
 * @className MemberService
 * @description //TODO
 * @data 2025/3/15 23:24
 */
@Service
public class MemberService {

    @Resource
    private MemberMapper memberMapper;
    public int count(){
        long l = memberMapper.countByExample(null);
        return (int)l;
    }

    public CommonResp regitster(MemberRegisterReq registerReq){
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(registerReq.getMobile());
        List<Member> list = memberMapper.selectByExample(memberExample);
        if (!list.isEmpty()) {
//            return list.get(0).getId();
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(registerReq.getMobile());

        memberMapper.insert(member);

        CommonResp<Long> longCommonResp = new CommonResp<>();
        longCommonResp.setSuccess(true);
        longCommonResp.setMessage("注册成功");
        longCommonResp.setContent(member.getId());
        return longCommonResp;

    }
}
